using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RockContorller : MonoBehaviour
{
    private float _speed=3;
    private float _rotationSpeed=5;

    private Vector3 moveDirection;
    private Vector3 rotationDirection;

    private Rigidbody _rb;
    
    [SerializeField]private GameObject _littleRock;
    
    private float maxPosx=10f;
    private float maxPosy=10f;
    void Start()
    {
        _rb=GetComponent<Rigidbody>();
        float directionX=Random.Range(-1f,1f);
        float directionY=Random.Range(-1f,1f);
        moveDirection=new Vector3(directionX,directionY,0);
        
        float rotationX=Random.Range(-1f,1f);
        float rotationY=Random.Range(-1f,1f);
        float rotationZ=Random.Range(-1f,1f);
        rotationDirection=new Vector3(rotationX,rotationY,rotationZ);
    }
    
    void Update()
    {
        // transform.Translate(moveDirection*_speed*Time.deltaTime,Space.World);
        // transform.Rotate(rotationDirection*_rotationSpeed*Time.deltaTime,Space.World);
        _rb.AddForce(moveDirection*_speed*Time.deltaTime,ForceMode.Impulse);
        _rb.AddTorque(rotationDirection*_rotationSpeed*Time.deltaTime,ForceMode.Impulse);

        if (Mathf.Abs(transform.position.x)>maxPosx||Mathf.Abs(transform.position.y)>maxPosy)
        {
            Destroy(gameObject);
        }
    }
    
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Bullet"))
        {
            if (this.CompareTag("Rock"))
            {
                Destroy(gameObject);
                Destroy(other.gameObject);
                int num=Random.Range(1,5);
                for (int i = 0; i < num; i++)
                {
                    Instantiate(_littleRock, transform.position, transform.rotation);
                }
                EventManager.AddSocre(1);
                AudioManager.instance.PlayMusic(1);
            }
            else
            {
                Destroy(other.gameObject);
                Destroy(gameObject);
                EventManager.AddSocre(2);
                AudioManager.instance.PlayMusic(1);
            }
        }
            
            
    }
}
